Spaces:
Sleeping
Sleeping
Commit
·
7359d1b
1
Parent(s):
0064263
Initial commit
Browse files- app.py +38 -32
- requirements.txt +1 -0
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import os
|
|
| 2 |
import logging
|
| 3 |
import sqlite3
|
| 4 |
from datetime import datetime
|
| 5 |
-
import
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
# تنظیم logging
|
|
@@ -11,10 +11,13 @@ logger = logging.getLogger(__name__)
|
|
| 11 |
|
| 12 |
# تنظیم API Key
|
| 13 |
logger.info("Starting application setup")
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
# تنظیم کلاینت OpenAI برای OpenRouter
|
| 20 |
try:
|
|
@@ -24,12 +27,12 @@ try:
|
|
| 24 |
)
|
| 25 |
# تست API Key
|
| 26 |
test_response = client.chat.completions.create(
|
| 27 |
-
model="
|
| 28 |
messages=[{"role": "user", "content": "test"}],
|
| 29 |
max_tokens=10,
|
| 30 |
extra_headers={
|
| 31 |
-
"HTTP-Referer": "https://
|
| 32 |
-
"X-Title": "Christopher
|
| 33 |
}
|
| 34 |
)
|
| 35 |
logger.info("✅ Initialized OpenAI client and API key validated for model qwen/qwen3-coder:free")
|
|
@@ -114,8 +117,8 @@ def generate_code(user_input, language):
|
|
| 114 |
temperature=0.7,
|
| 115 |
top_p=0.8,
|
| 116 |
extra_headers={
|
| 117 |
-
"HTTP-Referer": "https://
|
| 118 |
-
"X-Title": "Christopher
|
| 119 |
},
|
| 120 |
extra_body={}
|
| 121 |
)
|
|
@@ -159,31 +162,34 @@ def christopher(user_input, language, show_history):
|
|
| 159 |
history = get_history() if show_history else ""
|
| 160 |
return f"خطا: {e}", history
|
| 161 |
|
| 162 |
-
# رابط گرادیو
|
| 163 |
-
def gradio_ui(user_input, language, show_history):
|
| 164 |
-
code, history = christopher(user_input, language, show_history)
|
| 165 |
-
return code, history
|
| 166 |
-
|
| 167 |
if __name__ == "__main__":
|
| 168 |
logger.info("Starting application")
|
| 169 |
try:
|
| 170 |
init_db()
|
| 171 |
-
logger.info("🚀 Launching
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 187 |
except Exception as e:
|
| 188 |
logger.error(f"❌ Startup error: {e}")
|
| 189 |
-
|
|
|
|
| 2 |
import logging
|
| 3 |
import sqlite3
|
| 4 |
from datetime import datetime
|
| 5 |
+
import streamlit as st
|
| 6 |
from openai import OpenAI
|
| 7 |
|
| 8 |
# تنظیم logging
|
|
|
|
| 11 |
|
| 12 |
# تنظیم API Key
|
| 13 |
logger.info("Starting application setup")
|
| 14 |
+
# دریافت کلید از secrets
|
| 15 |
+
try:
|
| 16 |
+
openrouter_api_key = st.secrets["OPENROUTER_API_KEYY"]
|
| 17 |
+
logger.info("✅ API key loaded from Streamlit secrets")
|
| 18 |
+
except KeyError:
|
| 19 |
+
st.error("کلید API تعریف نشده! لطفاً از بخش Secrets در Hugging Face اضافه کنید.")
|
| 20 |
+
st.stop()
|
| 21 |
|
| 22 |
# تنظیم کلاینت OpenAI برای OpenRouter
|
| 23 |
try:
|
|
|
|
| 27 |
)
|
| 28 |
# تست API Key
|
| 29 |
test_response = client.chat.completions.create(
|
| 30 |
+
model="google/gemma-3n-e2b-it:free",
|
| 31 |
messages=[{"role": "user", "content": "test"}],
|
| 32 |
max_tokens=10,
|
| 33 |
extra_headers={
|
| 34 |
+
"HTTP-Referer": "https://huggingface.co/spaces",
|
| 35 |
+
"X-Title": "Christopher Streamlit App"
|
| 36 |
}
|
| 37 |
)
|
| 38 |
logger.info("✅ Initialized OpenAI client and API key validated for model qwen/qwen3-coder:free")
|
|
|
|
| 117 |
temperature=0.7,
|
| 118 |
top_p=0.8,
|
| 119 |
extra_headers={
|
| 120 |
+
"HTTP-Referer": "https://huggingface.co/spaces",
|
| 121 |
+
"X-Title": "Christopher Streamlit App"
|
| 122 |
},
|
| 123 |
extra_body={}
|
| 124 |
)
|
|
|
|
| 162 |
history = get_history() if show_history else ""
|
| 163 |
return f"خطا: {e}", history
|
| 164 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 165 |
if __name__ == "__main__":
|
| 166 |
logger.info("Starting application")
|
| 167 |
try:
|
| 168 |
init_db()
|
| 169 |
+
logger.info("🚀 Launching Streamlit interface")
|
| 170 |
+
|
| 171 |
+
# Streamlit UI
|
| 172 |
+
st.title("کریستوفر - دستیار کدنویسی")
|
| 173 |
+
st.write("توضیح برنامه را به فارسی وارد کنید و زبان برنامهنویسی را انتخاب کنید. برای نمایش تاریخچه، گزینه مربوطه را تیک بزنید.")
|
| 174 |
+
|
| 175 |
+
user_input = st.text_area("توضیح برنامه به فارسی (مثال: یک تابع اعداد زوج)", height=100)
|
| 176 |
+
language = st.selectbox("زبان برنامهنویسی", ["Python", "JavaScript", "Java", "C++"])
|
| 177 |
+
show_history = st.checkbox("نمایش تاریخچه درخواستها")
|
| 178 |
+
|
| 179 |
+
if st.button("تولید کد"):
|
| 180 |
+
if user_input.strip():
|
| 181 |
+
with st.spinner("در حال تولید کد..."):
|
| 182 |
+
generated_code, history = christopher(user_input, language, show_history)
|
| 183 |
+
|
| 184 |
+
st.subheader("کد تولیدشده:")
|
| 185 |
+
st.code(generated_code, language=language.lower())
|
| 186 |
+
|
| 187 |
+
if show_history and history:
|
| 188 |
+
st.subheader("تاریخچه درخواستها:")
|
| 189 |
+
st.text_area("تاریخچه", value=history, height=300)
|
| 190 |
+
else:
|
| 191 |
+
st.error("لطفاً توضیح برنامه را وارد کنید.")
|
| 192 |
+
|
| 193 |
except Exception as e:
|
| 194 |
logger.error(f"❌ Startup error: {e}")
|
| 195 |
+
st.error(f"خطا در راهاندازی:
|
requirements.txt
CHANGED
|
@@ -11,3 +11,4 @@ transformers
|
|
| 11 |
accelerate
|
| 12 |
gradio
|
| 13 |
openai
|
|
|
|
|
|
| 11 |
accelerate
|
| 12 |
gradio
|
| 13 |
openai
|
| 14 |
+
streamlit
|