Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,9 +4,34 @@ import os
|
|
| 4 |
from groq import Groq
|
| 5 |
from duckduckgo_search import DDGS
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# Function to convert JSON to formatted text
|
| 8 |
def json_to_text(filename):
|
| 9 |
try:
|
|
|
|
|
|
|
| 10 |
with open(filename, 'r') as json_file:
|
| 11 |
data = json.load(json_file)
|
| 12 |
return json.dumps(data, indent=2)
|
|
@@ -26,8 +51,8 @@ def search_duckduckgo(query):
|
|
| 26 |
|
| 27 |
# Function to chat with JSON content using Groq API and DuckDuckGo fallback
|
| 28 |
def chat_with_json(api_key, model_name, filename, question):
|
| 29 |
-
if not api_key or not filename or not question:
|
| 30 |
-
return "Please provide
|
| 31 |
|
| 32 |
try:
|
| 33 |
client = Groq(api_key=api_key)
|
|
@@ -36,11 +61,15 @@ def chat_with_json(api_key, model_name, filename, question):
|
|
| 36 |
|
| 37 |
prompt = f"JSON Data:\n{json.dumps(data, indent=2)}\n\nUser question: {question}\n\nAnswer based on the JSON data if possible. If the question cannot be answered based on the JSON, respond with 'SEARCH_WEB'."
|
| 38 |
|
| 39 |
-
|
| 40 |
model=model_name,
|
| 41 |
-
messages=[{"role": "user", "content": prompt}]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
)
|
| 43 |
-
answer =
|
| 44 |
|
| 45 |
if 'SEARCH_WEB' in answer:
|
| 46 |
return search_duckduckgo(question)
|
|
@@ -51,30 +80,34 @@ def chat_with_json(api_key, model_name, filename, question):
|
|
| 51 |
except Exception as e:
|
| 52 |
return f"Error with Groq API: {str(e)}"
|
| 53 |
|
| 54 |
-
#
|
| 55 |
def get_json_files():
|
| 56 |
-
|
|
|
|
| 57 |
|
| 58 |
-
# Gradio Interface
|
| 59 |
iface = gr.Interface(
|
| 60 |
fn=json_to_text,
|
| 61 |
inputs=gr.Dropdown(choices=get_json_files(), label="Select JSON File"),
|
| 62 |
outputs=gr.Markdown(),
|
| 63 |
-
title="JSON to Text"
|
|
|
|
| 64 |
)
|
| 65 |
|
|
|
|
| 66 |
chat_iface = gr.Interface(
|
| 67 |
fn=chat_with_json,
|
| 68 |
inputs=[
|
| 69 |
-
gr.Textbox(label="Enter Groq API Key", type="password"),
|
| 70 |
gr.Dropdown(choices=groq_models, label="Select Model"),
|
| 71 |
gr.Dropdown(choices=get_json_files(), label="Select JSON File"),
|
| 72 |
gr.Textbox(label="Ask your question")
|
| 73 |
],
|
| 74 |
outputs=gr.Markdown(),
|
| 75 |
title="Chat with JSON and DuckDuckGo",
|
| 76 |
-
description="
|
| 77 |
)
|
| 78 |
|
|
|
|
| 79 |
if __name__ == "__main__":
|
| 80 |
gr.TabbedInterface([iface, chat_iface], ["JSON to Text", "Chat with JSON"]).launch()
|
|
|
|
| 4 |
from groq import Groq
|
| 5 |
from duckduckgo_search import DDGS
|
| 6 |
|
| 7 |
+
# List of Groq models (defined globally to avoid NameError)
|
| 8 |
+
groq_models = [
|
| 9 |
+
"qwen-2.5-32b",
|
| 10 |
+
"qwen-2.5-coder-32b",
|
| 11 |
+
"qwen-qwq-32b",
|
| 12 |
+
"deepseek-r1-distill-qwen-32b",
|
| 13 |
+
"deepseek-r1-distill-llama-70b",
|
| 14 |
+
"gemma2-9b-it",
|
| 15 |
+
"distil-whisper-large-v3-en",
|
| 16 |
+
"llama-3-1-8b-instant",
|
| 17 |
+
"llama-3-2-11b-vision-preview",
|
| 18 |
+
"llama-3.2-1b-preview",
|
| 19 |
+
"llama-3.2-3b-preview",
|
| 20 |
+
"llama-3.2-90b-vision-preview",
|
| 21 |
+
"llama-3.3-70b-specdec",
|
| 22 |
+
"llama-3.3-70b-versatile",
|
| 23 |
+
"llama-guard-3-8b",
|
| 24 |
+
"llama3-70b-8192",
|
| 25 |
+
"llama3-8b-8192",
|
| 26 |
+
"mistral-saba-24b",
|
| 27 |
+
"mixtral-8x7b-32768"
|
| 28 |
+
]
|
| 29 |
+
|
| 30 |
# Function to convert JSON to formatted text
|
| 31 |
def json_to_text(filename):
|
| 32 |
try:
|
| 33 |
+
if not filename:
|
| 34 |
+
return "Please select a JSON file."
|
| 35 |
with open(filename, 'r') as json_file:
|
| 36 |
data = json.load(json_file)
|
| 37 |
return json.dumps(data, indent=2)
|
|
|
|
| 51 |
|
| 52 |
# Function to chat with JSON content using Groq API and DuckDuckGo fallback
|
| 53 |
def chat_with_json(api_key, model_name, filename, question):
|
| 54 |
+
if not api_key or not model_name or not filename or not question:
|
| 55 |
+
return "Please provide all required inputs: API key, model, JSON file, and question."
|
| 56 |
|
| 57 |
try:
|
| 58 |
client = Groq(api_key=api_key)
|
|
|
|
| 61 |
|
| 62 |
prompt = f"JSON Data:\n{json.dumps(data, indent=2)}\n\nUser question: {question}\n\nAnswer based on the JSON data if possible. If the question cannot be answered based on the JSON, respond with 'SEARCH_WEB'."
|
| 63 |
|
| 64 |
+
completion = client.chat.completions.create(
|
| 65 |
model=model_name,
|
| 66 |
+
messages=[{"role": "user", "content": prompt}],
|
| 67 |
+
temperature=1,
|
| 68 |
+
max_completion_tokens=1024,
|
| 69 |
+
top_p=1,
|
| 70 |
+
stream=False # Set to False for simplicity; can be True for streaming
|
| 71 |
)
|
| 72 |
+
answer = completion.choices[0].message.content
|
| 73 |
|
| 74 |
if 'SEARCH_WEB' in answer:
|
| 75 |
return search_duckduckgo(question)
|
|
|
|
| 80 |
except Exception as e:
|
| 81 |
return f"Error with Groq API: {str(e)}"
|
| 82 |
|
| 83 |
+
# Function to get list of JSON files dynamically
|
| 84 |
def get_json_files():
|
| 85 |
+
json_files = [f for f in os.listdir('.') if f.endswith('.json')]
|
| 86 |
+
return json_files if json_files else ["No JSON files found"]
|
| 87 |
|
| 88 |
+
# Gradio Interface for JSON to Text
|
| 89 |
iface = gr.Interface(
|
| 90 |
fn=json_to_text,
|
| 91 |
inputs=gr.Dropdown(choices=get_json_files(), label="Select JSON File"),
|
| 92 |
outputs=gr.Markdown(),
|
| 93 |
+
title="JSON to Text",
|
| 94 |
+
description="Convert a JSON file to formatted text."
|
| 95 |
)
|
| 96 |
|
| 97 |
+
# Gradio Interface for Chat with JSON
|
| 98 |
chat_iface = gr.Interface(
|
| 99 |
fn=chat_with_json,
|
| 100 |
inputs=[
|
| 101 |
+
gr.Textbox(label="Enter Groq API Key", type="password"),
|
| 102 |
gr.Dropdown(choices=groq_models, label="Select Model"),
|
| 103 |
gr.Dropdown(choices=get_json_files(), label="Select JSON File"),
|
| 104 |
gr.Textbox(label="Ask your question")
|
| 105 |
],
|
| 106 |
outputs=gr.Markdown(),
|
| 107 |
title="Chat with JSON and DuckDuckGo",
|
| 108 |
+
description="Ask questions about a JSON file. If the answer isn’t in the JSON, it will search DuckDuckGo."
|
| 109 |
)
|
| 110 |
|
| 111 |
+
# Launch the tabbed interface
|
| 112 |
if __name__ == "__main__":
|
| 113 |
gr.TabbedInterface([iface, chat_iface], ["JSON to Text", "Chat with JSON"]).launch()
|