Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from groq import Groq
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
# Set your API key
|
6 |
+
os.environ["GROQ_API_KEY"] = "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"
|
7 |
+
|
8 |
+
# Initialize the client
|
9 |
+
client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
|
10 |
+
|
11 |
+
# Function to process document and prompt
|
12 |
+
def process_document(prompt, document):
|
13 |
+
# Combine the prompt and document
|
14 |
+
input_text = f"{prompt}\n\n{document}"
|
15 |
+
|
16 |
+
# Call the Groq API
|
17 |
+
try:
|
18 |
+
chat_completion = client.chat.completions.create(
|
19 |
+
messages=[{"role": "user", "content": input_text}],
|
20 |
+
model="llama3-8b-8192", # Replace with the model name you want to use
|
21 |
+
stream=False,
|
22 |
+
)
|
23 |
+
# Extract and return the result
|
24 |
+
return chat_completion.choices[0].message.content
|
25 |
+
except Exception as e:
|
26 |
+
return f"An error occurred: {e}"
|
27 |
+
|
28 |
+
# Create the Gradio interface
|
29 |
+
interface = gr.Interface(
|
30 |
+
fn=process_document,
|
31 |
+
inputs=[
|
32 |
+
gr.Textbox(lines=2, placeholder="Enter your prompt here", label="Prompt"),
|
33 |
+
gr.Textbox(lines=10, placeholder="Paste your document here", label="Document"),
|
34 |
+
],
|
35 |
+
outputs="text",
|
36 |
+
title="Groq Document Processor",
|
37 |
+
description="Input a prompt and a document to process using the Groq API.",
|
38 |
+
)
|
39 |
+
|
40 |
+
# Launch the app
|
41 |
+
interface.launch()
|
42 |
+
|